﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WRJ
{
	public partial class MainMenuDB : BaseForm
	{
		private static string db_name = "WRJDB.mdb";
		Login login;
		public MainMenuDB(Login log)
		{
			InitializeComponent();
			this.FormClosing += new FormClosingEventHandler(MainMenuDB_FormClosing);
			splitContainer1.SplitterDistance = 83;
			login = log;
			BackupFile(System.IO.Directory.GetCurrentDirectory(), db_name);

			ManageUserScore.Enabled = false;
		}

		void MainMenuDB_FormClosing(object sender, FormClosingEventArgs e)
		{
			DialogResult result = MessageBox.Show("确定要退出该系统吗？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
			if (result == DialogResult.OK)
			{
				e.Cancel = false;
				login.Close();
				Application.Exit();
			}
			else
			{
				e.Cancel = true;
			}
		}

		private void ManageDB_FormClosing(object sender, FormClosingEventArgs e)
		{
			DialogResult result = MessageBox.Show("确定要退出该系统吗？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
			if (result == DialogResult.OK)
			{
				e.Cancel = false;
				login.Close();
				Application.Exit();
			}
			else
			{
				e.Cancel = true;
			}
		}

		private void ExitProgram_Click(object sender, EventArgs e)
		{
			login.Close();
			Application.Exit();
		}

		private void ManageDB_Load(object sender, EventArgs e)
		{
		}

		private void ManageUser_Click(object sender, EventArgs e)
		{
			var collection = this.splitContainer1.Panel2.Controls.OfType<UserControl>();
			foreach (var item in collection)
			{
				item.Dispose();
			}
			var userControl = new ManageUser(this, ManageUserScore);
			userControl.Dock = DockStyle.Fill;
			this.splitContainer1.Panel2.Controls.Add(userControl);
		}

		private void ManageAdmin_Click(object sender, EventArgs e)
		{
			var collection = this.splitContainer1.Panel2.Controls.OfType<UserControl>();
			foreach (var item in collection)
			{
				item.Dispose();
			}
			var userControl = new ManageAdmin(this, ManageUserScore);
			userControl.Dock = DockStyle.Fill;
			this.splitContainer1.Panel2.Controls.Add(userControl);
		}


		private void ManageUserScore_Click(object sender, EventArgs e)
		{
			int UserID = Convert.ToInt32(this.splitContainer1.Panel2.Controls.OfType<ManageUser>().First().dataGridView1.CurrentRow.Cells[0].Value);
			string UserName = Convert.ToString(this.splitContainer1.Panel2.Controls.OfType<ManageUser>().First().dataGridView1.CurrentRow.Cells[1].Value);

			var collection = this.splitContainer1.Panel2.Controls.OfType<UserControl>();
			foreach (var item in collection)
			{
				item.Dispose();
			}
			var userControl = new ManageUserScore(this,ManageUserScore, UserName, UserID);
			userControl.Dock = DockStyle.Fill;
			this.splitContainer1.Panel2.Controls.Add(userControl);
		}

		private void ReturnProgram_Click(object sender, EventArgs e)
		{
			login.Show();
			this.Dispose();
		}

		/// <summary>
		/// 备份数据库文件，加上当前时间的后缀
		/// </summary>
		/// <param name="fileName"></param>
		private void BackupFile(string url, string fileName)
		{
			try
			{
				string str1 = url + "\\DB\\" + fileName;

				fileName = fileName + "_" + DateTime.Now.ToString();
				fileName = fileName.Replace(':', '_');
				fileName = fileName.Replace('-', '_');
				fileName = fileName.Replace(' ', '_');
				string str2 = url + "\\DB\\Backup\\" + fileName;

				System.IO.Directory.CreateDirectory(Path.GetDirectoryName(str2));
				System.IO.File.Copy(str1, str2);
			}
			catch (Exception e)
			{
				MessageBox.Show(e.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

	}
}
